package com.hzccz.kafka;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtils {
    private static final String DEFAULT_CONFIG_FILE = "kafka.properties";
    private static Properties properties;

    static {
        loadProperties(DEFAULT_CONFIG_FILE);
    }

    public static void loadProperties(String configFile) {
        properties = new Properties();
        try (InputStream input = new FileInputStream(configFile)) {
            properties.load(input);
            System.out.println("成功加载配置文件: " + configFile);
        } catch (IOException ex) {
            System.err.println("加载配置文件失败: " + configFile + ", 使用默认配置");
            
            setDefaultProperties();
        }
    }

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    public static int getIntProperty(String key, int defaultValue) {
        try {
            String value = properties.getProperty(key);
            return value != null ? Integer.parseInt(value) : defaultValue;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public static long getLongProperty(String key, long defaultValue) {
        try {
            String value = properties.getProperty(key);
            return value != null ? Long.parseLong(value) : defaultValue;
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    private static void setDefaultProperties() {
        
        properties.setProperty("bootstrap.servers", "localhost:9092");
        properties.setProperty("topic", "default-topic");

        properties.setProperty("producer.file.path", "data.txt");
        properties.setProperty("producer.delay.ms", "1000");
        properties.setProperty("producer.acks", "all");
        properties.setProperty("producer.retries", "3");
        properties.setProperty("producer.linger.ms", "1");

        properties.setProperty("consumer.group.id", "default-group");
        properties.setProperty("consumer.delay.ms", "1000");
        properties.setProperty("consumer.auto.offset.reset", "earliest");
    }
}
